package com.yixinsoft.basic.web.exception;

import java.util.HashMap;
import java.util.Map;

import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by hqy on 2016/06/09
 */
@ControllerAdvice
public class DefaultExceptionHandler {
	
	@ExceptionHandler({UnauthorizedException.class})
	@ResponseStatus(HttpStatus.UNAUTHORIZED)
    public ModelAndView processUnauthenticatedException(NativeWebRequest request, UnauthorizedException e) {
		Map<String, Object> model = new HashMap<String, Object>();  
        model.put("message", "未授权此操作，请联系管理员"); 
        return new ModelAndView("error/500", model); 
    }
	
	@ExceptionHandler(value = {Exception.class})
	@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
	public ModelAndView handleExceptions(Exception ex, WebRequest req) {
		Map<String, Object> model = new HashMap<String, Object>();  
        model.put("message", ex.getMessage());  
        return new ModelAndView("error/500", model);  
	}
	
}
